package member1;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Color;

public class Delete extends JPanel {
	private JLabel lblNewLabel;
	private JTextField username;
	private JButton search;
	private JTextArea textArea;
	private JButton remove;
	
	Data6[] data;
	int index;
	private JLabel lblNewLabel_1;
	boolean check = false;

	/**
	 * Create the panel.
	 */
	public Delete() {
		setBackground(new Color(51, 204, 204));
		setLayout(null);
		add(getLblNewLabel());
		add(getUsername());
		add(getSearch());
		add(getTextArea());
		add(getRemove());
		add(getLblNewLabel_1());

	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("이름 입력");
			lblNewLabel.setForeground(Color.WHITE);
			lblNewLabel.setFont(new Font("굴림", Font.PLAIN, 11));
			lblNewLabel.setBounds(41, 97, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getUsername() {
		if (username == null) {
			username = new JTextField();
			username.setBounds(118, 94, 149, 21);
			username.setColumns(10);
		}
		return username;
	}
	private JButton getSearch() {
		if (search == null) {
			search = new JButton("검색");
			search.setFont(new Font("굴림", Font.PLAIN, 11));
			search.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					boolean flag = true;
					
					if(!username.getText().equals("")) // 공백이 아니라면
					{
						for(int i=0;i<data.length;i++){
							if (data[i] == null)
								continue;
							
							else if (username.getText().equals(data[i].name)) // 이름 같은걸 찾는다
							{
								flag = false;
								check = true;
								
								index = i; // 인덱스를 얻어온다.
								textArea.setText(data[i].name+"	"+data[i].address+"	"+
										data[i].phone+"	"+data[i].email+"	"+data[i].age+"	");
								break;
							}
						}
					}
					
					if (flag)
						textArea.setText("데이터가 존재하지 않습니다.");
				}
			});
			search.setBounds(312, 93, 97, 23);
		}
		return search;
	}
	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
			textArea.setFont(new Font("Monospaced", Font.PLAIN, 11));
			textArea.setBounds(41, 131, 368, 24);
		}
		return textArea;
	}
	private JButton getRemove() {
		if (remove == null) {
			remove = new JButton("삭제");
			remove.setFont(new Font("굴림", Font.PLAIN, 11));
			remove.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					// 삭제 버튼을 누르면, 해당 행의 데이터를 삭제하고 배열을 정리한다. 

					if (check){
						for(int i=index;i<data.length;i++){
							if (i == data.length-1)
								data[i] = null;
							else
								data[i] = data[i+1];
							textArea.setText("삭제되었습니다.");
						}
					}
					else
						textArea.setText("데이터를 입력하지 않았습니다.");
				}
			});
			remove.setBounds(312, 183, 97, 23);
		}
		return remove;
	}
	
	public void setD (Data6[] data){
		this.data = data;  // 필드에서 선언된 data에 데이터를 저장해주는 작업 
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("데이터 삭제 페이지");
			lblNewLabel_1.setFont(new Font("나눔손글씨 펜", Font.PLAIN, 25));
			lblNewLabel_1.setBounds(152, 24, 149, 24);
		}
		return lblNewLabel_1;
	}
}
